enum PIN {
    //% block="P0"
    1,
    //% block="P1"
    2
}

//% color="#9F32F1" iconWidth=50 iconHeight=40
namespace servo_k10 {

    //% block="servo [PIN] angle[pos] " blockType="command" 
    //% PIN.shadow="dropdown" PIN.options="PIN"
    //% pos.shadow="range" pos.params.min=0 pos.params.max=180 pos.defl=90
    export function servo_angle(parameter: any, block: any) {
        let pin = parameter.PIN.code
        let pos = parameter.pos.code;
        Generator.addInclude("ESP32Servo", "#include <ESP32Servo.h>");
        Generator.addObject(`myservo_${pin}`, `Servo`, `myservo_${pin};`);
        Generator.addObject(`servoPin_${pin}`, `int`, `servoPin_${pin} = ${pin} ;`);   
        Generator.addSetup(`ESP32PWM0`, `ESP32PWM::allocateTimer(0);`);
        Generator.addSetup(`ESP32PWM1`, `ESP32PWM::allocateTimer(1);`);
        Generator.addSetup(`ESP32PWM2`, `ESP32PWM::allocateTimer(2);`);
        Generator.addSetup(`ESP32PWM3`, `ESP32PWM::allocateTimer(3);`);
        Generator.addSetup(`setPeriodHertz_${pin}`, `myservo_${pin}.setPeriodHertz(50);`);
        Generator.addSetup(`attach_${pin}`, `myservo_${pin}.attach(servoPin_${pin}, 500, 2500);`);
        Generator.addCode(`myservo_${pin}.write(${pos});`);
    }
}

